package com.huitone.gddw.controller.monitoring.warn;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.InnormalEvent;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.WarnEventTrace;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.monitoring.base.IInnormalEventService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 监控异常事件管理
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="innormalEvent")
public class InnormalEventController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(InnormalEventController.class);
	
	@Autowired
	private IInnormalEventService innormalEventService;
	
	/**
	 * 查询
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<Map<String, Object>> query(QueryPage<InnormalEvent> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询预警明细";
		QueryPage<Map<String, Object>> result = null;
		try {
			page.setParams(getParams(request));
			result = innormalEventService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询预警明细信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String objType = request.getParameter("objType");
		String dealStatus = request.getParameter("dealStatus");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("objType", objType);
		params.put("dealStatus", dealStatus);
		return params;
	}
	
	/**
	 * 处理异常信息
	 * @param entity InnormalEvent
	 * @param warnEventTrace warnEventTrace
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="change")
	public Message change(InnormalEvent entity, WarnEventTrace warnEventTrace) {
		try {
			InnormalEvent innormalEvent = innormalEventService.getById(warnEventTrace.getEventId());
			innormalEvent.setDealStatus(entity.getDealStatus());
			innormalEventService.update(innormalEvent, warnEventTrace);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("处理异常信息过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "保存失败！");
		}
	}
	
}
